package com.shengde.jinwawaforleader.model.myJob.onlineEducation;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;

/**
 * @author 鞠永强
 * @version $Rev$
 * @time 2017/2/27 0027 14:26
 * @des ${公共资源 和本园资源的 界面}
 * @updateAuthor $Author$
 * @updateDate $Date$
 * @updateDes ${TODO}
 */

public class PublicSourceActivity extends BaseActivity {
    private TextView tv_back; //
    private TextView tv_title;
    public WebView show_wv;
    private LinearLayout loading_main;  //加载中..
    private String type;

    @Override
    public void onRoot(Bundle savedInstanceState) {
        setContentView(R.layout.activity_public_source);
    }

    @Override
    public void initView() {
        loading_main = (LinearLayout) findViewById(R.id.loading_main);
        tv_back = (TextView) findViewById(R.id.tv_back);
        tv_title = (TextView) findViewById(R.id.tv_title);
        show_wv = (WebView) findViewById(R.id.show_wv);
    }

    @Override
    public void initData() {
        type = getIntent().getStringExtra("type");
        if ("1".equals(type)) {
            tv_title.setText(getString(R.string.public_source));
        } else if ("2".equals(type)) {
            tv_title.setText(getString(R.string.school_myself));
        }
        WebSettings settings = show_wv.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setSupportZoom(false);
        settings.setBuiltInZoomControls(false);
        showPublicSource();
    }

    /**
     * 加载公共资源
     */
    private void showPublicSource() {

        //主要是为了让webview在自己的app中加载 如不去第三方浏览器
        //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
        show_wv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                loading_main.setVisibility(View.GONE);
            }

        });
        if ("1".equals(type)) {
            show_wv.loadUrl("http://appweb.babaxiong.com/android/index_n.html?v=1487656360608");
        } else if ("2".equals(type)) {
            show_wv.loadUrl("http://m.tom61.com/");
        }
    }

    @Override
    public void initListener() {
        tv_back.setOnClickListener(this);
    }

    @Override
    public void onWidgetClick(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                finish();
                break;
            default:
                break;
        }
    }

    //为了让 webview的返回不finsh()当前的Activity
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && show_wv.canGoBack()) {
            show_wv.goBack(); //goBack()表示返回WebView的上一页面
            return true;
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        show_wv.stopLoading();
        show_wv.destroy();
        super.onDestroy();
    }
}
